package org.bemuv;

import org.bemuv.util.BemuvUtil;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;


public class HomeActivity extends FragmentActivity {

	private Button btn_exit;
	private Button btn_favorites;
	private Button btn_messages;
	private Button btn_shop;
	private Button btn_happy_hour;
	private String current="";
	private String type="";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
			
		btn_exit = (Button) findViewById(R.id.btn_exit);
		btn_favorites = (Button) findViewById(R.id.btn_favorites);
		btn_messages = (Button) findViewById(R.id.btn_messages);
		btn_shop = (Button) findViewById(R.id.btn_setting_shop);
		btn_happy_hour = (Button) findViewById(R.id.btn_happy_hour);

		SearchFragment search=(SearchFragment)getSupportFragmentManager().findFragmentById(R.id.frg_search);
		BemuvUtil.setTitle(search.getActivity(), R.drawable.ic_home, getResources().getString(R.string.title_activity_home));

		//--------------------------------------------------------------------------------------
				ParseUser currentUser = ParseUser.getCurrentUser();
				current = (String) currentUser.get("username");
		   			ParseQuery<ParseObject> query = ParseQuery.getQuery("userShop");
		   			query.whereEqualTo("userID", current);
		   			query.getFirstInBackground(new GetCallback<ParseObject>() {
		   			  public void done(ParseObject object1, ParseException e) {
		   			    if (object1 == null) {
		   			    } else {
		   			    	
		   			 	if (object1.get("userType").equals("sh")){
		   	   				type = object1.get("userType").toString();
		   	   			
		   	   				btn_shop.setText("Administrar Tienda");   	   				
		   	   			}
		   			    	
		   			    }
		   			  }
		   			});
		   			 
				
		   		
		   		//---------------------------------------------------------------------------------------------	
				
				
				btn_exit.setOnClickListener(new OnClickListener() {
					public void onClick(View view) {
						exit(null);
					}
				});
				
				btn_favorites.setOnClickListener(new OnClickListener() {
					public void onClick(View view) {
						launch_favorites(null);
					}
				});
				
				btn_messages.setOnClickListener(new OnClickListener() {
					public void onClick(View view) {
						launch_messages(null);
					}
				});
				
				btn_shop.setOnClickListener(new OnClickListener() {
					public void onClick(View view) {
					if (type.equals("sh")){
					launch_settingshop(null);
					}else{
						launch_shop(null);}
					}
					
				});
				
				btn_happy_hour.setOnClickListener(new OnClickListener() {
					public void onClick(View view) {
						launch_happy_hour(null);
					}
				});
			}
			
			
			public void exit(View view) {
				ParseUser.logOut();
				ParseUser currentUser = ParseUser.getCurrentUser();
				Intent i = new Intent(getApplicationContext(), SignInActivity.class);
				startActivity(i);
				this.finish();
			}

			public void launch_favorites(View view) {
				Intent i = new Intent(getApplicationContext(), FavoritesActivity.class);
				startActivity(i);
			}
			
			public void launch_messages(View view) {
				Intent i = new Intent(getApplicationContext(), MessagesActivity.class);
				startActivity(i);
			}
			
			public void launch_shop(View view) {
				Intent i = new Intent(getApplicationContext(), AddShopActivity.class);
				startActivity(i);
			}
			
			public void launch_settingshop(View view) {
				Intent i = new Intent(getApplicationContext(), SettingShopActivity.class);
				startActivity(i);
			}
			
			public void launch_happy_hour(View view) {
				Intent i = new Intent(getApplicationContext(), QueryActivity.class);
				startActivity(i);
			}
			@Override
			public boolean onCreateOptionsMenu(Menu menu) {
				// Inflate the menu; this adds items to the action bar if it is present.
				getMenuInflater().inflate(R.menu.activity_home, menu);
				return true;
			}

		}
